<?php

include('pagina.php');
include('database.php');
include('auth.php');
include('core.php');

// Verifica se esta autenticado com pelo menos nivel 3
validaPermissao(3);

// Verifica se ha expense para ver
if (!$_GET['id'])
{
	forbidden();
}

// Busca todos os dados do expense
$expense = query_fetch("SELECT * FROM duedates WHERE id='{$_GET['id']}' AND value < 0 LIMIT 1");

// Se nao existe, cai fora
if (!$expense)
{
    response_404();
}

// Trata acoes
if ($_GET['paid'] == 1 AND $expense['paid'] == FALSE)
{
	query_bd("UPDATE duedates SET paid=TRUE WHERE id={$expense['id']}");
	$expense['paid'] = TRUE;
	$_GET['msg'] = 'The expense was registered as paid in the system.';
}
else if ($_GET['cancel'] == 1)
{
	if ($expense['references_to'] == CONTACT)
		query_bd("DELETE FROM contacts WHERE id={$expense['reference_id']} LIMIT 1");

	query_bd("DELETE FROM duedates WHERE id={$expense['id']} LIMIT 1");
	header( "Location: incomes_expenses.php?msg=The previous expense was excluded from the system." );
	exit;
}

// Inicia cabecalho da pagina
pagina_inicio('ParkSys - Expense');
pagina_head();

// Opcoes
echo "<ul id=opcoes>";
if ($expense['paid'] == FALSE)
	echo "<li class='positive'><a href='expense.php?id={$expense['id']}&amp;paid=1' ",
	"onClick='return confirm(\"Do you really want to set this expense as paid?\");'>",
	"<img src='img/ok.png' alt=''>Register expense as paid</a></li>";

echo "<li class='negative'><a href='expense.php?id={$expense['id']}&amp;cancel=1' ",
"onClick='return confirm(\"Do you really want to cancel this expense?\");'>",
"<img src='img/cancel.png' alt=''>Cancel expense(exclude)</a></li>";
echo "<li><a href='expense.php?id={$_GET['id']}&amp;print=1' target='_blank' ><img src='img/print.png' alt=''>Print</a></li>";
echo "</ul><br>";

// Mostra dados do income
echo "<div class=dados_estaticos>";

echo "<h1>Expense</h1><br>";
echo "<dl>";

echo "<dt>Name</dt><dd>{$expense['name']}",
	($expense['paid'] == FALSE? "<span class=negativo>(not paid)</span>" : ""), "</dd>";
echo "<dt>Description</dt><dd>{$expense['description']}</dd>";
echo "<dt>Category</dt><dd>", $EXP_CATEGORIES[$expense['category']], "</dd>";
echo "<dt>Due date</dt><dd>", date("M j Y", strtotime($expense['payday'])), "</dd>";
echo "<dt>Value</dt><dd>", show_money($expense['value']), "</dd>";

switch ($expense['references_to'])
{
	case EMPLOYEE:
		$employee = query_fetch("SELECT employees.id, contacts.name FROM employees, contacts WHERE " .
			"employees.id={$expense['reference_id']} AND contacts.id=employees.contact_id LIMIT 1");
		echo "<dt>Employee</dt><dd><a href='employee.php?id={$employee['id']}'>{$employee['name']}</a></dd>";
		break;
	case CUSTOMER:
		$customer = query_fetch("SELECT customers.id, contacts.name FROM customers, contacts WHERE " .
			"customers.id={$expense['reference_id']} AND contacts.id=customers.contact_id LIMIT 1");
		echo "<dt>Customer</dt><dd><a href='customer.php?id={$customer['id']}'>{$customer['name']}</a></dd>";
		break;
	case LOCATION:
		$location = query_fetch("SELECT locations.id, contacts.name FROM locations, contacts WHERE " .
			"locations.id={$expense['reference_id']} AND contacts.id=locations.locationcontact LIMIT 1");
		echo "<dt>Location</dt><dd><a href='location.php?id={$location['id']}'>{$location['name']}</a></dd>";
		break;
	case REPORT:
		$report = query_fetch("SELECT id FROM reports WHERE id={$expense['reference_id']} LIMIT 1");
		echo "<dt>Report</dt><dd><a href='report.php?id={$report['id']}'><img src='img/report.png' alt='report'></a></dd>";
		break;
	case EVENT:
		$event = query_fetch("SELECT reports.id, events.name FROM events, reports WHERE " .
			"reports.id={$expense['reference_id']} AND reports.special_event=FALSE AND events.id=reports.location_event_id LIMIT 1");
		echo "<dt>Event</dt><dd><a href='report.php?id={$event['id']}'>{$event['name']}</a></dd>";
		break;
	case CONTACT:
		$contact = query_fetch("SELECT * FROM contacts WHERE id={$expense['reference_id']} LIMIT 1");
		echo "<dd><hr><h2>Contact</h2></dd>";
		echo "<dt>Address</dt><dd><address>",
		"{$contact['number']}, {$contact['street']}, {$contact['complement']} ",
		"zip: {$contact['zipcode']}, {$contact['city']} - {$contact['state']}.</address></dd>";
		if ($contact['phone'])
			echo "<dt>Phone number</dt><dd>{$contact['phone']}</dd>";
		if ($contact['cellphone'])
			echo "<dt>Mobile number</dt><dd>{$contact['cellphone']}</dd>";
		if ($contact['email'])
			echo "<dt>Email</dt><dd>{$contact['email']}</dd>";
		break;
}

if ($expense['doc_number'])
	echo "<dt>Document number</dt><dd>{$expense['doc_number']}</dd>";
if ($expense['obs'])
	echo "<dt>Observations</dt><dd>{$expense['obs']}</dd>";

echo "</dl>";
echo "</div>";

// Finaliza pagina
pagina_fim();
?>
